import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 使用线程池的服务端
 */
public class TCPThreadPoolServer extends TCPEchoServer{
    public TCPThreadPoolServer(int port) throws IOException {
        super(port);
    }

    @Override
    public void start() throws IOException {
        System.out.println("TCP客户端已启动...");
        //创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                10,
                1,
                TimeUnit.MICROSECONDS,
                new LinkedBlockingDeque<>(10));
        while (true) {
            Socket clientSocket = socket.accept();
            threadPoolExecutor.submit(() -> {
                processConnections(clientSocket);
            });
        }
    }

    public static void main(String[] args) throws IOException {
        TCPThreadPoolServer tcpThreadServer = new TCPThreadPoolServer(7777);
        tcpThreadServer.start();
    }
}
